package org.palmelf.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import org.palmelf.model.MyBatisSessionFactory;
import org.palmelf.model.Phontograph;

/**
 * 照片接口实现
 * 
 * @author caizh
 * 
 */
public class PhontographServiceImpl implements PhontographService {
	// private Logger log = Logger.getLogger(PhontographServiceImpl.class);

	/**
	 * 
	 * 查看所有的信息
	 */
	public List<Phontograph> fetchPhontographList() {
		SqlSession session = MyBatisSessionFactory.getSqlSessionFactory()
				.openSession();
		List<Phontograph> list = session
				.selectList("phontograph.fetchPhontographList");
		session.close();
		return list;
	}

	/**
	 * 分页查询
	 */
	public List<Phontograph> fetchPhontographByPage(int page, int rows,
			Phontograph phontograph) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("firstResult", page);
		parameter.put("maxResult", rows);
		parameter.put("phontograph", phontograph);

		SqlSession session = MyBatisSessionFactory.getSqlSessionFactory()
				.openSession();
		List<Phontograph> list = session.selectList(
				"phontograph.fetchPhontographByPage", parameter);
		session.close();
		return list;
	}

	/**
	 * 新增信息
	 */
	public void addPhontograph(String imgName) {
		// System.out.println("addPhontograph:"+phontograph.getpName());
		Phontograph phontograph = new Phontograph();
		phontograph.setpDate(new Date());
		phontograph.setpPhonto(imgName);
		phontograph.setpName(imgName.substring(imgName.lastIndexOf("."),
				imgName.length()));
		phontograph.setCarNum("");
		SqlSession session = MyBatisSessionFactory.getSqlSessionFactory()
				.openSession();
		session.insert("phontograph.addPhontograph", phontograph);
		session.commit();
		session.close();
	}

	/**
	 * 接收车牌号，修改最后一条记录的车牌号
	 * 
	 * @param carNum
	 *            车牌号
	 */
	public void updatePhontograph(String carNum) {
		SqlSession session = MyBatisSessionFactory.getSqlSessionFactory()
				.openSession();
		Phontograph phontograph = fetchPhontographList().get(0);
		phontograph.setCarNum(carNum);
		session.insert("phontograph.updatePhontograph", phontograph);
		session.commit();
		session.close();
	}

	public Phontograph fetchPhontograph() {
		// TODO Auto-generated method stub
		return null;
	}
}
